/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dto;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import exceptions.TrackException;
import gui.data.ITrackCellData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.DefaultListModel;

/**
 *
 * @author steffen
 */
@DatabaseTable(tableName = "track")
public class Track implements ITrackCellData {

    public static final String NAME_FIELD_NAME = "name";
    public static final String DATE_FIELD_NAME = "date";
    public static final String START_FIELD_NAME = "start";
    public static final String DES_FIELD_NAME = "destination";
    public static final String GPX_FIELD_NAME = "gpxfilepath";
    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false)
    private String name;
    @DatabaseField(persisted = false)
    private DefaultListModel<Event> events;             // Werden erst beim auslesen hinzugefügt
    @DatabaseField(foreign = true)
    private Metadata metadata;
    @DatabaseField(columnName = DATE_FIELD_NAME)
    private long date; //timespamp
    @DatabaseField(columnName = START_FIELD_NAME)
    private String start;
    @DatabaseField(columnName = DES_FIELD_NAME)
    private String destination;
    @DatabaseField(columnName = GPX_FIELD_NAME)
    private String gpxFilePath;
    private ArrayList<org.alternativevision.gpx.beans.Track> trackList;

    public Track() {
    } //Empty constructor needed for ormlite

    public Track(String name) {
        this.name = name;
    }

    public String getGpxFilePath() {
        return gpxFilePath;
    }

    public Track(String name, Event[] events) {
        this(name, Arrays.asList(events));
    }

    public Track(String name, List<Event> events) {
        this.name = name;

        this.events = new DefaultListModel<>();
        if (events != null) {
            for (Event e: events) {
                this.events.addElement(e);
            }
        }
    }

    @Override
    public int hashCode() {
        int hash = 5;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Track other = (Track) obj;
        return true;
    }

    public void setGpxFilePath(String gpxFilePath) {
        this.gpxFilePath = gpxFilePath;
    }

    public Track(String name, Calendar date) {
        this.name = name;
        this.date = date.getTimeInMillis();
    }

    public ArrayList<org.alternativevision.gpx.beans.Track> getTrackList() {
        return trackList;
    }

    public void setTrackList(ArrayList<org.alternativevision.gpx.beans.Track> trackList) {
        this.trackList = trackList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public DefaultListModel<Event> getEvents() {
        return events;
    }

    public void setEvents(List<Event> events) {
        if (this.events == null) {
            this.events = new DefaultListModel<>();
        } else {
            this.events.clear();
        }

        for (Event e: events) {
            this.events.addElement(e);
        }
    }

    public void addEvent(Event event) {
        if (this.events == null) {
            this.events = new DefaultListModel<>();
        }

        this.events.addElement(event);
    }

    public void removeEvent(Event event) {
        if (this.events == null) {
            return;
        }

        this.events.removeElement(event);
    }

    public Calendar getDate() {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(date);
        return cal;
    }

    public void setDate(Calendar date) throws TrackException {
        if (date == null) {
            throw new TrackException(TrackException.EMPTY_DATE);
        }
        this.date = date.getTimeInMillis();
    }

    public Metadata getMetadata() {
        return metadata;
    }

    public void setMetadata(Metadata metadata) {
        if (metadata == null) {
            throw new RuntimeException("Metadata empty");
        }
        this.metadata = metadata;
    }

    /**
     * Returns the name of the Track
     */
    public String getTrackName() {
        return name;
    }

    /**
     * Returns the Distance of the Track as an float
     */
    public float getTrackDistance() {
        return getMetadata().getDistance();
    }

    /**
     * Returns the Date when the Track was
     */
    public Calendar getTrackDate() {
        return getDate();
    }
}
